//
//  DiscoverViewController.m
//  MELive
//
//  Created by 曹曹 on 2016/10/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DiscoverViewController.h"
#import "WYScrollView.h"
#import "AFNetworking.h"
#import "Macro.h"
#import "GYX_RootDataModel.h"
#import "GYX_FatherData.h"
#import "GYX_SonData.h"
#import "GYX_BannerCarousel.h"
#import "GYX_DiscoverData.h"
#import "GYX_OutDoorsCollectionViewCell.h"
#import "GYX_LiveList.h"
#import "GYX_SelectCycleViewController.h"
#import "GYX_MoreLiveViewController.h"
#import "GYX_SearchBarViewController.h"
#import "GYX_RoomCoverDetailList.h"
#import "GYX_CollectionData.h"
#import "GYX_RootLiveModel.h"
#import "YYModel.h"
@interface DiscoverViewController ()<UISearchBarDelegate, WYScrollViewNetDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UISearchBar *BYsearchTextFd;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) GYX_RootDataModel *rootData;
@property (nonatomic, strong) GYX_DiscoverData *banner;
@property (nonatomic, strong) GYX_RootLiveModel *data;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self creatCollectionView];
    [self cycleHandleData];
}
- (void)cycleHandleData {
    NSMutableArray *dataSource = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"5i/VuFkC/YmERzrU1p9QtMValxiXI3GB,fPS5IFcwBgyhOSBZqe6NZjhQqF7vC8tcBVZEtW5w8sOQ/GAVA9sUALk9QTuC /hPEi1ybxO49iuqnoH8Zw+edqla9E5UqqRHk6SKh5wZmGHsGpBLfMNJJhZ85 V8G6tcjk6PUbDBzq2/AbTX7iHICh9g==" forHTTPHeaderField:@"X-Authorization"];
    [manager POST:@"http://me.yy.com:80/main/getTemplateFindData" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        self.rootData = [GYX_RootDataModel modelWithDictionary:dic];
        _banner = [[GYX_DiscoverData alloc] init];
        _banner.data = [NSArray arrayWithArray:self.rootData.data.data[1].data];
        for (GYX_BannerCarousel *carousel in _banner.data) {
            [dataSource addObject:carousel.picUrl];
            self.BYsearchTextFd = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,44)];
            self.BYsearchTextFd.placeholder = @"搜索";
            self.BYsearchTextFd.barStyle = UIBarStyleDefault;
            self.BYsearchTextFd.delegate = self;
            self.BYsearchTextFd.searchBarStyle = UISearchBarStyleProminent;
            UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            searchBtn.frame = self.BYsearchTextFd.frame;
            [searchBtn addTarget:self action:@selector(clickSearchBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self.BYsearchTextFd addSubview:searchBtn];
            WYScrollView *cycleView = [[WYScrollView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 150) WithNetImages:dataSource];
            [cycleView setAutoScrollDelay:2];
            cycleView.netDelagate = self;
            UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(0, -194, SCREEN_WIDTH, 194 * 2)];
            [bigView addSubview:self.BYsearchTextFd];
            [bigView addSubview:cycleView];
            [_collectionView addSubview:bigView];
        }
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
    }];
}
- (void)didSelectedNetImageAtIndex:(NSInteger)index {
    GYX_SelectCycleViewController *cycle = [[GYX_SelectCycleViewController alloc] init];
    cycle.urlStr = [_banner.data[index].linkUrl description];
    cycle.webTitle = _banner.data[index].title;
    cycle.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:cycle animated:YES];
}
- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"961c99a0-e574-4054-98cd-6e0f20bcc62b" forHTTPHeaderField:@"token"];
    
    [manager GET:@"http://api1.naonaola.com/room/latest/v2?page=0&pageSize=20" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        _data = [GYX_RootLiveModel modelWithDictionary:dic];
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
    }];
}
- (void)creatCollectionView {
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.minimumLineSpacing = 5;
    _flowLayout.minimumInteritemSpacing = 5;
    _flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 5) / 2.0, 250);
    _flowLayout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:_flowLayout];
    _collectionView.contentInset = UIEdgeInsetsMake(194, 0, 0, 0);
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"GYX_OutDoorsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell1"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _data.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYX_OutDoorsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.data = _data.data[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    CATransform3D rotation; //3D旋转
    rotation = CATransform3DMakeTranslation(0, 50, 20);
    rotation = CATransform3DScale(rotation, 0.9, 0.9, 1);
    rotation.m34 = 1.0 / 600;
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    cell.layer.transform = rotation;
    [UIView beginAnimations:@"rotation" context:NULL];
    //旋转时间
    [UIView setAnimationDuration:0.6];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}
- (void)clickSearchBtn:(UIButton *)searchBar {
    GYX_SearchBarViewController *search = [[GYX_SearchBarViewController alloc] init];
    search.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:search animated:YES];
}
@end
